package com.carefree.bookmall.entity;

import com.carefree.bookmall.common.utils.PriceUtil;

import java.io.Serializable;
import java.util.*;

/**
 * @author qianyue
 * @email qianyue1226@gmail.com
 * @date 2022-03-21 20:11:28
 */
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    private String id;
    /**
     * 总金额
     */
    private Double total;
    /**
     * 数量
     */
    private Integer amount;
    /**
     * 状态
     */
    private Integer status;
    /**
     * 支付类型
     */
    private Integer paytype;
    /**
     * 用户id
     */
    private Integer userId;
    /**
     * 真实姓名
     */
    private String realName;
    /**
     * 电话
     */
    private String phone;
    /**
     * 地址
     */
    private String address;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 修改时间
     */
    private Date updateTime;

    private Map<Integer, OrderItem> itemMap = new HashMap<Integer, OrderItem>();

    private List<OrderItem> itemList = new ArrayList<OrderItem>();


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getPaytype() {
        return paytype;
    }

    public void setPaytype(Integer paytype) {
        this.paytype = paytype;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Map<Integer, OrderItem> getItemMap() {
        return itemMap;
    }

    public void setItemMap(Map<Integer, OrderItem> itemMap) {
        this.itemMap = itemMap;
    }

    public List<OrderItem> getItemList() {
        return itemList;
    }

    public void setItemList(List<OrderItem> itemList) {
        this.itemList = itemList;
    }

    public void addGoods(Book book) {
        if (itemMap.containsKey(book.getId())) {
            OrderItem item = itemMap.get(book.getId());
            item.setAmount(item.getAmount() + 1);
        } else {
            OrderItem item = new OrderItem();
            item.setPrice(book.getPrice());
            item.setAmount(1);
            item.setBook(book);
            item.setOrder(this);
            item.setBookId(book.getId());
            itemMap.put(book.getId(), item);
        }
        amount++;

        total = PriceUtil.add(total, book.getPrice());
    }

    public void lessen(Integer bookId) {
        if (itemMap.containsKey(bookId)) {
            OrderItem item = itemMap.get(bookId);
            item.setAmount(item.getAmount() - 1);
            amount--;
            total = PriceUtil.subtract(total, item.getPrice());
            if (item.getAmount() <= 0) {
                itemMap.remove(bookId);
            }
        }
    }

    public void delete(Integer bookId) {
        if (itemMap.containsKey(bookId)) {
            OrderItem item = itemMap.get(bookId);
            total = PriceUtil.subtract(total, item.getAmount() * item.getPrice());
            amount -= item.getAmount();
            itemMap.remove(bookId);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (! (o instanceof Order)) return false;
        Order order = (Order) o;
        return Objects.equals(getId(), order.getId()) && Objects.equals(getTotal(), order.getTotal()) && Objects.equals(getAmount(), order.getAmount()) && Objects.equals(getStatus(), order.getStatus()) && Objects.equals(getPaytype(), order.getPaytype()) && Objects.equals(getUserId(), order.getUserId()) && Objects.equals(getRealName(), order.getRealName()) && Objects.equals(getPhone(), order.getPhone()) && Objects.equals(getAddress(), order.getAddress()) && Objects.equals(getCreateTime(), order.getCreateTime()) && Objects.equals(getUpdateTime(), order.getUpdateTime()) && Objects.equals(getItemMap(), order.getItemMap()) && Objects.equals(getItemList(), order.getItemList());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getTotal(), getAmount(), getStatus(), getPaytype(), getUserId(), getRealName(), getPhone(), getAddress(), getCreateTime(), getUpdateTime(), getItemMap(), getItemList());
    }

    @Override
    public String toString() {
        return "Order{" +
                "id='" + id + '\'' +
                ", total=" + total +
                ", amount=" + amount +
                ", status=" + status +
                ", paytype=" + paytype +
                ", userId=" + userId +
                ", realName='" + realName + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", itemMap=" + itemMap +
                ", itemList=" + itemList +
                '}';
    }
}
